package cn.yh.hospitalappointmentsystemdemo.controller;

import cn.yh.hospitalappointmentsystemdemo.common.AjaxResult;
import cn.yh.hospitalappointmentsystemdemo.common.TableDataInfo;
import cn.yh.hospitalappointmentsystemdemo.controller.base.BaseController;
import cn.yh.hospitalappointmentsystemdemo.domain.entity.PreAppointments;
import cn.yh.hospitalappointmentsystemdemo.domain.query.PreAppointmentCreate;
import cn.yh.hospitalappointmentsystemdemo.mapping.AppointmentMapping;
import cn.yh.hospitalappointmentsystemdemo.service.IAppointmentService;
import cn.yh.hospitalappointmentsystemdemo.utils.JWTUtils;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.yh.hospitalappointmentsystemdemo.domain.query.PageQuery;

@Api(tags = "预约管理")
@RestController
@RequestMapping("/appointments")
public class AppointmentController extends BaseController {
    @Autowired
    private IAppointmentService appointmentService;

    @ApiOperation(value = "分页获取预约列表")
    @GetMapping
    public TableDataInfo getAppointments(PageQuery pageQuery) {
        Page page = appointmentService.page(pageQuery, JWTUtils.getUserIdByClaim());
        return dataInfo(page.getRecords(), page.getTotal());
    }

    @ApiOperation("新增预约")
    @PostMapping
    public AjaxResult addPreAppointment(@RequestBody PreAppointmentCreate preAppointmentCreate){
        preAppointmentCreate.setPatientId(JWTUtils.getUserIdByClaim());
        System.out.println(preAppointmentCreate);
        System.out.println(AppointmentMapping.INSTANCE.toCreate(preAppointmentCreate));
        return toAjax(appointmentService.save(AppointmentMapping.INSTANCE.toCreate(preAppointmentCreate)));
    }

    @DeleteMapping("/{preAppointmentId}")
    public AjaxResult deletePreAppointment(@PathVariable Long preAppointmentId){
        System.out.println(preAppointmentId);
        if (!appointmentService.removeById(preAppointmentId)){
            return AjaxResult.error("取消失败");
        }
        return AjaxResult.success();
    }

    @PutMapping("/cancel/{preAppointmentId}")
    public AjaxResult cancelAppointment(@PathVariable Long preAppointmentId){
        LambdaUpdateWrapper<PreAppointments> lqw = Wrappers.lambdaUpdate();
        lqw.eq(PreAppointments::getPreAppointmentId,preAppointmentId)
                .set(PreAppointments::getStatus,"canceled");
        if (!appointmentService.update(lqw)){
            return AjaxResult.error("取消失败");
        }
        return AjaxResult.success();
    }

    @GetMapping("/getPreAppointments")
    public TableDataInfo getPreAppointments(PageQuery pageQuery){
        Long doctorId = JWTUtils.getUserIdByClaim();
        Page page = appointmentService.page1(pageQuery,doctorId);
        return dataInfo(page.getRecords(),page.getTotal());
    }

    @PutMapping("/confirm/{preAppointmentId}")
    public AjaxResult confirmAppointment(@PathVariable Long preAppointmentId){
        LambdaUpdateWrapper<PreAppointments> lqw = Wrappers.lambdaUpdate();
        lqw.eq(PreAppointments::getPreAppointmentId,preAppointmentId)
                .set(PreAppointments::getStatus,"confirmed");
        if (!appointmentService.update(lqw)){
            return AjaxResult.error("确认失败");
        }
        return AjaxResult.success();
    }

    @PutMapping("/reject/{preAppointmentId}")
    public AjaxResult rejectAppointment(@PathVariable Long preAppointmentId){
        LambdaUpdateWrapper<PreAppointments> lqw = Wrappers.lambdaUpdate();
        lqw.eq(PreAppointments::getPreAppointmentId,preAppointmentId)
                .set(PreAppointments::getStatus,"rejected");
        if (!appointmentService.update(lqw)){
            return AjaxResult.error("拒绝失败");
        }
        return AjaxResult.success();
    }
}
